package com.kangyi.admin.controller;

import com.kangyi.admin.service.PublicActivityService;
import com.kangyi.dto.AddPublicActivityDTO;
import com.kangyi.dto.EditPublicActivityDTO;
import com.kangyi.dto.PublicActivityPageDTO;
import com.kangyi.result.PublicActivityPageResult;
import com.kangyi.result.Result;
import com.kangyi.vo.PublicActivityDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@RestController
@RequestMapping("/admin/publicActivity")
@Slf4j
@Api(tags = "超级管理员-公益活动管理相关接口")
public class PublicActivityController {

    @Autowired
    private PublicActivityService publicActivityService;


    @GetMapping("/page")
    @ApiOperation("活动管理分页查询")
    public Result<PublicActivityPageResult> pageQuery(PublicActivityPageDTO publicActivityPageDTO){
        log.info("活动管理分页查询:{}",publicActivityPageDTO);
        try {
            PublicActivityPageResult publicActivityPageResult = publicActivityService.pageQuery(publicActivityPageDTO);
            return Result.success(publicActivityPageResult,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    @ApiOperation("发布活动")
    @CacheEvict(cacheNames = "publicActivity_", allEntries = true)
    public Result addPublicActivity(@RequestBody AddPublicActivityDTO addPublicActivityDTO){
        log.info("发布活动:{}",addPublicActivityDTO);
        try {
            publicActivityService.addPublicActivity(addPublicActivityDTO);
            return Result.success("发布成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("查询发布活动详情")
//    @Cacheable(cacheNames = "publicActivity_", key = "'adminDetail'+#id")
    public Result<PublicActivityDetailVO> detailPublicActivity(@PathVariable Integer id){
        log.info("查询发布活动详情:{}",id);
        try {
            PublicActivityDetailVO publicActivityDetailVO = publicActivityService.detailPublicActivity(id);
            return Result.success(publicActivityDetailVO,"查询成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @PutMapping("/edit")
    @ApiOperation("编辑公益活动")
    @CacheEvict(cacheNames = "publicActivity_", allEntries = true)
    public Result editPublicActivity(@RequestBody EditPublicActivityDTO editPublicActivityDTO){
        log.info("编辑公益活动:{}",editPublicActivityDTO);
        try {
            publicActivityService.editPublicActivity(editPublicActivityDTO);
            return Result.success("修改成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除公益活动")
    @CacheEvict(cacheNames = "publicActivity_", allEntries = true)
    public Result deletePublicActivity(@RequestParam List<Integer> ids){
        log.info("批量删除公益活动:{}",ids);
        try {
            publicActivityService.deletePublicActivity(ids);
            return Result.success("删除成功！");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
